// token管理
import { getToken, setToken, removeToken } from '@/utils/auth'
import { loginAPI } from '@/api/user'
// import { resetRouter } from '@/router'

const state = () => {
  return {
    token: getToken() || ''
  }
}
const mutations = {
  // 设置token
  setToken: (state, token) => {
    state.token = token
    // 本地再存一份token
    setToken(token)
  },
  removeToken: (state) => {
    state.token = ''
    // 清空本地token
    removeToken()
  }

}
const actions = {
  // 登录action
  loginAction: async({ commit }, loginForm) => {
    // 发登录请求
    const resp = await loginAPI(loginForm)
    console.log(resp)
    // 提交mutation
    commit('setToken', resp.token)
  },
  // 退出登录
  logoutAction: ({ commit }) => {
    commit('removeToken')

    // // 重置路由实例
    // resetRouter()
    // // 清空vuex中路由
    // commit('route/removeRoutes', null, {
    //   // 从全局出发
    //   root: true })
  }

}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
